package com.actions.bluetoothbox2.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.actions.bluetoothbox2.R;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyong on 2017/12/25.
 */

public class PopupVoiceSelecter2 extends PopupWindow implements View.OnClickListener {

    private boolean[] isCheck;
    private onSelecteListener selected;
    private int pos = 0;
    private int voiceID;
    private List<VoiceInfo> list_voice;
    private PopupVoiceSelecter2Adapter adapter;

    public PopupVoiceSelecter2(Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.pop_voiceselect_layout, null);

        ListView pop_list = (ListView) view.findViewById(R.id.pop_list);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_ok = (Button) view.findViewById(R.id.btn_ok);

        list_voice = new ArrayList<>();
        adapter = new PopupVoiceSelecter2Adapter(context, list_voice);
        pop_list.setAdapter(adapter);
        pop_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int j = 0; j < list_voice.size(); j++) {
                    if(j == i){
                        list_voice.get(j).ischecked = true;
                    }else {
                        list_voice.get(j).ischecked = false;
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });

        btn_cancel.setOnClickListener(this);
        btn_ok.setOnClickListener(this);

        setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public void setList_voice(List<VoiceInfo> list) {
        list_voice.clear();
        list_voice.addAll(list);
        for (int i = 0; i < list_voice.size(); i++) {
            if(i==0){
                list_voice.get(i).ischecked = true;
            }else  list_voice.get(i).ischecked = false;
        }
        adapter.notifyDataSetChanged();
    }

    public void setSelected(onSelecteListener selected) {
        this.selected = selected;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_ok:
                this.dismiss();
                if (null != selected) {
                    for (int i = 0; i < list_voice.size(); i++) {
                        if (list_voice.get(i).ischecked) {
                            selected.onSelected(list_voice.get(i));
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    public interface onSelecteListener {
        void onSelected(VoiceInfo info);
    }

}
